const wx2my = require('../../wx2my');

// pages/shop/shop.js
const api = require('../../utils/user_api_miniprogram.js');

const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    wrap: true,
    acceptRule: false,
    taglist: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let that = this;
    that.getMap('getPointHistory').then(res => {
      console.log(res);
      that.setData({
        taglist: res.records
      });
    });
  },

  toPayPage() {
    var prodid = e.currentTarget.dataset.prodid;

    if (prodid) {
      wx2my.navigateTo({
        url: '/pages/prod/prod?prodid=' + prodid
      });
    } // wx.navigateToMiniProgram({
    //     appId: 'wxee14dc16b9a70aa8',						
    //     path: 'pages/index/index',	
    //     extraData: {},
    //     success(res) {
    //       // 打开成功
    //     },
    //     fail(res){
    //       //打开失败
    //     //   wx.switchTab({
    //     //     url: '/pages/user_index/user_index',
    //     //   })
    //   }
    //   })
    // wx.navigateTo({
    //     url: '/pages/user_paymentOrder/user_paymentOrder?type=2',
    // })

  },

  toProdPage: function (e) {
    let that = this; // that.getTmplID()

    let msgToken = wx2my.getStorageSync('token');
    console.log(111);

    if (msgToken == '' || msgToken == null) {
      that.setData({
        wrap: false
      });
      return;
    }

    var prodid = e.currentTarget.dataset.prodid;

    if (prodid) {
      wx2my.navigateTo({
        url: '/pages/prod/prod?prodid=' + prodid
      });
    } // wx.navigateToMiniProgram({
    //     appId: 'wxee14dc16b9a70aa8',						
    //     path: 'pages/index/index',	
    //     extraData: {},
    //     success(res) {
    //       // 打开成功
    //     },
    //     fail(res){
    //       //打开失败
    //     //   wx.switchTab({
    //     //     url: '/pages/user_index/user_index',
    //     //   })
    //   }
    //   })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},

  getMap(url) {
    return new Promise(function (resolve, reject) {
      wx2my.request({
        url: app.globalData.host+'/prod/prodListByTagId?tagId=1&size=6',
        //接口地址
        header: {
          'content-type': 'application/json' // 默认值

        },

        success(res) {
          // 返回值
          resolve(res.data.data); // return res.data.data
        }

      });
    });
  },

  // 同意规则
  bindAcceptRule: function (e) {
    let that = this;
    that.data.acceptRule = !that.data.acceptRule;

    if (that.data.acceptRule == true) {
      this.acceptRule = true;
    } else {
      this.acceptRule = false;
    }
  },
  // 授权登陆
  getUserProfile: function (e) {
    console.log(e);
    let that = this;

    if (that.data.acceptRule) {
      wx2my.showLoading({
        title: '正在登录'
      });
      app.fm.wx_login((flag, userInfo) => {
        if (flag == 2) {
          that.setData({
            wrap: true
          });

          if (that.data.coupon_id) {
            that.get_coupon();
          }

          wx2my.hideLoading();
        } else if (flag == 3) {
          wx2my.hideLoading();
        }
      }, app);
    } else {
      app.toast("请点击同意《用户协议》");
    }
  },

  //扫码获取优惠券
  get_coupon() {
    let that = this;
    app.wxRequest('receive_coupon', {
      'coupon_id': that.data.coupon_id
    }, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        app.wxRequest('scan_code', {
          'coupon_id': that.data.coupon_id
        }, function (res) {
          console.log(res);

          if (res.data.code == 1) {
            that.setData({
              money: res.data.data.money,
              end_time: res.data.data.end_time,
              have_coupon: false
            });
          }
        });
      } else {
        app.toast(res.data.msg);
      }
    });
  },

  del() {
    this.setData({
      have_coupon: true,
      wrap: true,
      model: '',
      macno: '',
      rfid: '',
      tradeid: '',
      tradeno: ''
    });
  }

});